<template>
	<view>
		<view class="setting-list">
			<clxDialog :isShow='show' @dialogConfirm='dialogConfirm' @dialogCancel='dialogCancel'
				:content="dialogContent" :title="title"></clxDialog>
			<logoutclxDialog :isShow='logout_show' @dialogConfirm='logout_dialogConfirm'
				@dialogCancel='logout_dialogCancel' :title="logout_title"></logoutclxDialog>
			<view class="row" @click="myAddress">
				<view class="left">
					<text lines="1" class="text">我的地址</text>
				</view>
				<image class="arrow"
					src="@appstatic/img/my/my_icon_right_arrow.png">
			</view>

			<view class="row" @click="privacyAgreement">
				<view class="left">
					<text lines="1" class="text">协议/条款</text>
				</view>
				<image class="arrow"
					src="@appstatic/img/my/my_icon_right_arrow.png">
			</view>

			<view class="row" @click="clean">
				<view class="left">
					<text lines="1" class="text">清理缓存</text>
				</view>
				<view class="left">
					<text lines="1" class="text-1" style="color: #BDBDBD;">{{fileSizeString}}</text>
					<image class="arrow"
						src="@appstatic/img/my/my_icon_right_arrow.png">
				</view>
			</view>

			<view class="row" @click="currentVersionCheck">
				<view class="left">
					<text lines="1" class="text">当前版本</text>
				</view>
				<view class="left">
					<text lines="1" v-if="haveNewVersion" class="text-1"
						style="color: #EB5757;  font-weight: bold;">NEW</text>
					<text lines="1" class="text-1" style="padding-left: 10px;color: #BDBDBD;">V{{version}}</text>
					<image class="arrow"
						src="@appstatic/img/my/my_icon_right_arrow.png">
				</view>
			</view>
			
			<view class="row" @click="cancelAccount"
				v-if="userInfo !=null&&userInfo!=undefined&&userInfo!=''&&userInfo!='{}'">
				<view class="left">
					<text lines="1" class="text">注销账号</text>
				</view>
				<image class="arrow"
					src="https://pfmotordev.cq4oss.ctyunxs.cn/appstatic/img/my/my_icon_right_arrow.png">
			</view>

			<view class="row" @click="logout" v-if="userInfo !=null&&userInfo!=undefined&&userInfo!=''&&userInfo!='{}'">
				<view class="left">
					<text lines="1" class="text">退出登录</text>
				</view>
				<image class="arrow"
					src="@appstatic/img/my/my_icon_right_arrow.png">
			</view>
		</view>
	 
	</view>
</template>

<script>
	const app = getApp();
	const permission = require("../../../utils/permission.js");
	import clxDialog from "../../../components/clx-dialog/clx-dialog";
	import logoutclxDialog from "../../../components/clx-dialog/clx-dialog";
	// import updater from "../../../components/Updater/updater";
	const network = require("../../../manager/network-manager.js");
	const util = require("@utils/util.js");

	import checkUpdate from '@/components/uni-upgrade-center-app/utils/check-update'

	export default {
		data() {
			return {
				title: '确定清除缓存？',
				show: false,
				dialogContent: '是否确认清理程序缓存？缓存包括图片，视频等。',
				fileSizeString: '',
				version: '',
				haveNewVersion: false,
				logout_show: false,
				logout_title: '是否退出登录？',
				userInfo: null,
				env:'prd',
			}
		},
		created() {
			this.formatSize()
		},
		components: {
			clxDialog,
			logoutclxDialog
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			console.log("onLoad");

		},
		onShow: function() {
			console.log("onShow");
			if (app.globalData.userInfo) {
				this.userInfo = app.globalData.userInfo
			} else {
				this.userInfo = null
			}
			this.env = app.globalData.config.env;

			var that = this;
			// #ifdef APP-PLUS
			plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
				console.log("setting widgetInfo："+JSON.stringify(widgetInfo));
				that.version = widgetInfo.version;
			});
			// #endif
			// this.queryLastVersion();
		},
		props: {},
		methods: {
			//我的地址
			myAddress: function(e) {
				var that = this;
				if (util.isLogin(getApp())) {
					uni.navigateTo({
						url: '/module_my/pages/myAddress/myAddress'
					});
				} else {
					util.jumpLogin("myAddress");
				}
			},
			//接收推送通知
			receivePushMsg: function(e) {
				permission.gotoAppPermissionSetting();

			},
			//协议/条款
			privacyAgreement: function(e) {
				uni.navigateTo({
					url: "/module_my/pages/setting/agreementTerms",
				});
			},
			queryLastVersion: function(e) {

				var that = this;
				var uri = app.globalData.config.interfaces.URL_GETW_NEW_VERSION;
				var params = {
					"userId": app.getUserId()
				};
				let platform = uni.getSystemInfoSync().platform

				network.get(uri, params, 'form').then(res => {

					var data = res.data;
					if (res.code == 200) {
						console.log(data)
						//提示升级
						// i
						var updateInfo = {};

						if (platform == 'ios') {
							updateInfo = data.IOS;
						} else {
							updateInfo = data.android;
						}
						console.log("updateInfo:" + JSON.stringify(updateInfo));
						if (updateInfo && util.compareVersion(updateInfo.version, that.version)) {
							that.haveNewVersion = true;

						}
					}

				}).catch(res => {

				});
			},

			//展示退出
			logout_dialogConfirm() {
				this.logout_show = false
				app.saveUser(null);
				app.saveToken(null);
				uni.navigateBack({
					delta: 1
				})
			},
			logout_dialogCancel() {
				this.logout_show = false
			},
			//清理缓存
			clean: function(e) {
				this.show = true
			},
			dialogConfirm() {
				this.show = false
				this.clearCache()
			},
			// 获取缓存
			formatSize() {
				let that = this;
				plus.cache.calculate(function(size) {
					let sizeCache = parseInt(size);
					if (sizeCache == 0) {
						that.fileSizeString = "0B";
					} else if (sizeCache < 1024) {
						that.fileSizeString = sizeCache + "B";
					} else if (sizeCache < 1048576) {
						that.fileSizeString = (sizeCache / 1024).toFixed(2) + "KB";
					} else if (sizeCache < 1073741824) {
						that.fileSizeString = (sizeCache / 1048576).toFixed(2) + "MB";
					} else {
						that.fileSizeString = (sizeCache / 1073741824).toFixed(2) + "GB";
					}
				});
			},
			// 清理缓存
			clearCache() {
				let that = this;
				let os = plus.os.name;
				if (os == 'Android') {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					let len = files.length;
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
						plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
							if (entry.isDirectory) {
								entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录  
									uni.showToast({
										title: '清除缓存成功',
										icon: 'none', //如果要纯文本，不要icon，将值设为'none'
										duration: 2000 //持续时间为 2秒
									})

									that.formatSize(); // 重新计算缓存  
								}, function(e) {
									console.log(e.message)
								});
							} else {
								entry.remove();
							}
						}, function(e) {
							console.log('文件路径读取失败')
						});
					}
				} else { // ios  
					plus.cache.clear(function() {
						uni.showToast({
							title: '清除缓存成功',
							icon: 'none', //如果要纯文本，不要icon，将值设为'none'
							duration: 2000 //持续时间为 2秒
						})
						that.formatSize();
					});
				}
			},
			dialogCancel() {
				this.show = false
			},

			//当前版本
			currentVersionCheck: function(e) {
				var promis = checkUpdate();
				promis.then(res => {
						console.log(res)
					}).catch(res => {
						uni.showToast({
						title: res,
						icon: 'none', 
						duration: 2000 
					});
				});
			},
			logout: function(e) {
				this.logout_show = true

			},
			//注销账户
			cancelAccount: function() {
				uni.navigateTo({
					url: '/module_my/pages/setting/cancelAccount'
				})
			},
			
		}
	}
</script>

<style>
	@import "./setting.css";
</style>
